<template>
  <div class="header"
       :class="{'open-operation': isOperation, 'hide-button': userId}">
    <img class="header-seat"
         src="./../../assets/images/header/header-seat.jpg"
         alt="">
    <div class="header-default"
         v-if="themeConfig.moduleTheme === 'default'">
      <div class="header-left">
        <div class="header-left-weather">
          <img class="header-left-img"
               src="./../../assets/images/header/default/title-message.png"
               alt="">
          <div class="weather-content">
            <div class="weather-content-left">
              <img class="weather-img"
                   :src="weather.imgSrc"
                   alt="">
              <span class="site">{{ weather.currentCity }}</span>
            </div>
            <div class="weather-content-center">
              <span class="temperature"
                    ref="temperature">
                <i class="temperatureI"
                   ref="temperatureI">{{ weather.temperature }}°</i>
                <b class="weatherB"
                   ref="weatherB">
                  <i class="weatherI"
                     :class="{'move': weather.weatherMove}"
                     ref="weatherI">{{weather.weatherMessage}}</i>
                </b>
              </span>
              <span class="pm">PM2.5 : {{ weather.PM25 }}</span>
            </div>
          </div>
        </div>
        <div class="header-left-date">
          <img class="header-left-img"
               src="./../../assets/images/header/default/title-message-short.png"
               alt="">
          <div class="date-content">
            <p class="day">{{ date.date }}</p>
            <div class="time">
              <span class="week-span">{{ date.week }}</span>
              <span class="time-span">{{ date.time }}</span>
            </div>
          </div>
        </div>
      </div>
      <div class="header-center">
        <div class="header-center-content">
          <img class="header-img-seat"
               src="./../../assets/images/header/default/title.png"
               alt="">
          <div class="company-name"
               @mouseenter="openOperation"
               @mouseleave="closeOperation">
            <p>{{ isIndex ? companyName : pageName }}</p>
          </div>
        </div>
        <div class="header-operation"
             @mouseenter="openOperation"
             @mouseleave="closeOperation">
          <div class="header-operation-button">
            <span class="edit-button"
                  @click="editBtn">
              <i class="icon"></i>
              <i class="message">编辑名称</i>
            </span>
            <span class="organ-button"
                  @click="showAccountList">
              <i class="icon"></i>
              <i class="message">选择组织</i>
            </span>
          </div>
          <img class="header-option-img"
               src="./../../assets/images/header/header-content-bottom.png"
               alt="">
        </div>
      </div>
      <div class="header-right">
        <div class="header-right-setting"
             v-if="isIndex">
          <img @click="opensSetting"
               src="./../../assets/images/header/default/setting.png"
               alt="">
          <span class="buttons-tips">模块配置</span>
        </div>
        <div class="header-right-setting"
             v-else>
          <img class="setting"
               src="./../../assets/images/header/default/back.png"
               @click="backPage" />
          <span class="buttons-tips">返回首页</span>
        </div>
        <div class="header-right-screen"
             v-if="isEnlarge">
          <img @click="fullScreen"
               v-if="isEnlarge"
               src="./../../assets/images/header/default/narrow-screen.png"
               alt="">
          <span class="buttons-tips">缩小屏幕</span>
        </div>
        <div class="header-right-screen"
             v-else>
          <img @click="fullScreen"
               src="./../../assets/images/header/default/full-screen.png"
               alt="">
          <span class="buttons-tips">放大屏幕</span>
        </div>
      </div>
    </div>
    <div class="header-flat"
         v-else>
      <div class="header-left">
        <img class="header-img-seat"
             src="./../../assets/images/header/flat/title-message.png"
             alt="">
        <div class="header-left-message">
          <img :src="weather.imgSrc" />
          <span class="temperature-span">{{ weather.temperature }}°C</span>
          <span>{{ weather.weatherMessage }}</span>
          <span>PM2.5：{{ weather.PM25 }}</span>
          <span>{{ weather.currentCity }}</span>
        </div>
      </div>
      <div class="header-center">
        <div class="header-center-content">
          <img class="header-img-seat"
               src="./../../assets/images/header/flat/title.png"
               alt="">
          <div class="company-name"
               @mouseenter="openOperation"
               @mouseleave="closeOperation">
            <p>{{ isIndex ? companyName : pageName }}</p>
          </div>
        </div>
        <div class="header-operation"
             @mouseenter="openOperation"
             @mouseleave="closeOperation">
          <div class="header-operation-button">
            <span class="edit-button"
                  @click="editBtn">
              <i class="icon"></i>
              <i class="message">编辑名称</i>
            </span>
            <span class="organ-button"
                  @click="showAccountList">
              <i class="icon"></i>
              <i class="message">选择组织</i>
            </span>
          </div>
          <img class="header-option-img"
               src="./../../assets/images/header/header-content-bottom.png"
               alt="">
        </div>
      </div>
      <div class="header-right">
        <img class="header-img-seat"
             src="./../../assets/images/header/flat/title-message.png"
             alt="">
        <div class="header-right-message">
          <div class="header-right-content">
            <span>{{ date.date }}</span>
            <span>{{ date.week }}</span>
            <span class="date-time"> {{ date.time }}</span>
          </div>
          <div class="header-right-button">
            <div class="header-right-setting"
                 v-if="isIndex">
              <img class="setting"
                   src="./../../assets/images/header/flat/setting.png"
                   @click="opensSetting" />
              <span class="buttons-tips">模块配置</span>
            </div>
            <div class="header-right-setting"
                 v-else>
              <img class="setting"
                   src="./../../assets/images/header/flat/back.png"
                   @click="backPage" />
              <span class="buttons-tips">返回首页</span>
            </div>
            <div class="header-right-screen"
                 v-if="isEnlarge">
              <img class="full-screen"
                   src="./../../assets/images/header/flat/narrow-screen.png"
                   @click="fullScreen" />
              <span class="buttons-tips">缩小屏幕</span>
            </div>
            <div class="header-right-screen"
                 v-else>
              <img class="full-screen"
                   src="./../../assets/images/header/flat/full-screen.png"
                   @click="fullScreen" />
              <span class="buttons-tips">放大屏幕</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getDate } from './../../utils/common'
export default {
  name: 'headerVue',
  props: {
    companyName: {
      type: String,
      default: ''
    },
    userId: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      weather: {
        weatherMove: false, // 天气是否滚动
        currentCity: '',
        imgSrc: '',
        weatherMessage: '',
        temperature: '',
        PM25: ''
      },
      date: {
        time: '12:00',
        date: '2019-06-10',
        week: '星期一'
      },
      isOperation: false, // 是否打开操作
      isEnlarge: false // 是否全屏
    }
  },
  computed: {
    themeConfig() {
      return this.$store.state.themeConfig
    },
    isIndex() {
      return this.$store.state.isIndex
    },
    pageName() {
      return this.$store.state.pageName
    }
  },
  methods: {
    // 打开组织机构
    showAccountList() {
      this.$parent.showAccountList()
    },
    // 编辑头部名字
    editBtn() {
      this.$parent.editCompanyName()
    },
    // 打开操作
    openOperation() {
      if (this.userId || !this.isIndex) {
        this.isOperation = false
      } else {
        this.isOperation = true
      }
    },
    // 关闭操作
    closeOperation() {
      this.isOperation = false
    },
    // 打开设置
    opensSetting() {
      this.$store.commit('CHANGETHEMECONFIG', { type: 'isSetting', newData: true })
    },
    // 全屏
    fullScreen() {
      let self = this
      this.isEnlarge = !this.isEnlarge
      const docElm = document.body
      if (document.fullscreen || document.mozFullScreen || document.webkitIsFullScreen || document.msFullscreenElement) {
        if (document.exitFullscreen) {
          document.exitFullscreen()
          if (this.systemIdentification === 'android') {
            document.getElementsByClassName('home')[0].style.transform = ''
            document.getElementsByClassName('home')[0].style.transformOrigin = '0 0'
            setTimeout(function () {
              self.$nextTick(function () {
                // self.$parent.resizeChart()
              }, 500)
            })
          }
          return
        } else if (document.mozCancelFullScreen) {
          document.mozCancelFullScreen()
          if (this.systemIdentification === 'android') {
            document.getElementsByClassName('home')[0].style.transform = ''
            document.getElementsByClassName('home')[0].style.transformOrigin = '0 0'
            setTimeout(function () {
              self.$nextTick(function () {
                // self.$parent.resizeChart()
              }, 500)
            })
          }
          return
        } else if (document.webkitCancelFullScreen) {
          document.webkitCancelFullScreen()
          if (this.systemIdentification === 'android') {
            document.getElementsByClassName('home')[0].style.transform = ''
            document.getElementsByClassName('home')[0].style.transformOrigin = '0 0'
            setTimeout(function () {
              self.$nextTick(function () {
                // self.$parent.resizeChart()
              }, 500)
            })
          }
          return
        } else if (document.msExitFullscreen) {
          document.msExitFullscreen()
          if (this.systemIdentification === 'android') {
            document.getElementsByClassName('home')[0].style.transform = ''
            document.getElementsByClassName('home')[0].style.transformOrigin = '0 0'
            setTimeout(function () {
              self.$nextTick(function () {
                // self.$parent.resizeChart()
              }, 500)
            })
          }
          return
        }
      }
      if (docElm.requestFullscreen) {
        docElm.requestFullscreen()
        document.getElementsByClassName('home')[0].style.transform = ''
        document.getElementsByClassName('home')[0].style.transformOrigin = '0 0'
        setTimeout(function () {
          self.$nextTick(function () {
            self.$parent.resizeChart()
          }, 500)
        })
      } else if (docElm.mozRequestFullScreen) { // fireFox
        docElm.mozRequestFullScreen()
        document.getElementsByClassName('home')[0].style.transform = ''
        document.getElementsByClassName('home')[0].style.transformOrigin = '0 0'
        setTimeout(function () {
          self.$nextTick(function () {
            self.$parent.resizeChart()
          }, 500)
        })
      } else if (docElm.webkitRequestFullScreen) { // Chrome等
        docElm.webkitRequestFullScreen()
        document.getElementsByClassName('home')[0].style.transform = ''
        document.getElementsByClassName('home')[0].style.transformOrigin = '0 0'
        setTimeout(function () {
          self.$nextTick(function () {
            self.$parent.resizeChart()
          }, 500)
        })
      } else if (docElm.msRequestFullscreen) { // IE11
        docElm.msRequestFullscreen()
        document.getElementsByClassName('home')[0].style.transform = ''
        document.getElementsByClassName('home')[0].style.transformOrigin = '0 0'
        setTimeout(function () {
          self.$nextTick(function () {
            self.$parent.resizeChart()
          }, 500)
        })
      }
      if (this.systemIdentification === 'android') {
        if (window.screen.width < 1580) {
          let scale = window.screen.width / 1580
          document.getElementsByClassName('home')[0].style.transform = 'scale(' + scale + ')'
          document.getElementsByClassName('home')[0].style.transformOrigin = '0 0'
        } else {
          document.getElementsByClassName('home')[0].style.transform = ''
          document.getElementsByClassName('home')[0].style.transformOrigin = '0 0'
        }
        setTimeout(function () {
          self.$nextTick(function () {
            self.$parent.resizeChart('scale')
          }, 500)
        })
      }
    },
    // 获取天气信息
    getWeather() {
      this.commonApi.getWeather().then((res) => {
        if (res.data.code === '0') {
          let obj = {}
          let data = JSON.parse(res.data.data).results[0]
          let start = data.weather_data[0].date.indexOf('(') + 4
          let end = data.weather_data[0].date.length - 2
          let temperature = data.weather_data[0].date.substring(start, end)
          obj.currentCity = data.currentCity
          obj.PM25 = data.pm25
          obj.temperature = temperature
          obj.imgSrc = data.weather_data[0].dayPictureUrl
          obj.weatherMessage = data.weather_data[0].weather
          this.weather = obj
          if (this.$store.state.themeConfig.moduleTheme === 'default') {
            this.$nextTick(function () {
              let allWid = this.$refs.temperature.offsetWidth
              let temperatureIWid = this.$refs.temperatureI.offsetWidth
              let weatherBWid = allWid - temperatureIWid - allWid * 0.1
              if (this.$refs.weatherI.offsetWidth > weatherBWid) {
                this.weather.weatherMove = true
              } else {
                this.weather.weatherMove = false
              }
              this.$refs.weatherB.style.width = weatherBWid + 'px'
            })
          }
        }
      })
    },
    // 获取时间
    getNowTime() {
      let self = this
      let obj = getDate('yyyy-mm-dd')
      self.date.date = obj.days
      self.date.ackemma = obj.ackemma
      self.date.time = obj.hours + ':' + obj.minute
      self.date.week = obj.week
      clearInterval(window.changeTime)
      window.changeTime = setInterval(function () {
        let obj = getDate('yyyy-mm-dd')
        self.date.date = obj.days
        self.date.ackemma = obj.ackemma
        self.date.time = obj.hours + ':' + obj.minute
        self.date.week = obj.week
      }, 1000)
    },
    // 返回首页
    backPage() {
      this.$router.push({ path: '/home' })
      this.$store.commit('CHANGEPAGEINDEX', true)
    }
  },
  mounted() {
    // this.getWeather()
    this.getNowTime()
  }
}
</script>

<style lang="scss" scoped>
.header {
  position: relative;
  padding: 0;
  font-size: 14px;
  color: #a1cfff;
  transition: padding 0.2s ease-out;
  &.hide-button {
    .header-right-setting {
      display: none !important;
    }
  }
  &.open-operation {
    padding: 0 0 3%;
    transition: padding 0.2s ease-out;
    .header-center {
      overflow: inherit;
      .header-operation {
        opacity: 1;
        transition: opacity 0.1s linear 0.1s;
      }
    }
  }
  .header-seat {
    width: 100%;
    opacity: 0;
    display: block;
  }
  .header-flat {
    width: 100%;
    position: absolute;
    top: 0;
    left: 0;
    font-size: 14px;
    .header-img-seat {
      width: 100%;
      display: block;
    }
    .header-left {
      width: 29.375%;
      float: left;
      position: relative;
      .header-left-message {
        width: 100%;
        height: 100%;
        position: absolute;
        left: 0;
        top: 0;
        padding-left: 10px;
        display: flex;
        align-items: center;
        justify-content: flex-start;
        span {
          display: inline-block;
          float: left;
          margin-right: 25px;
          height: 26px;
          line-height: 26px;
          justify-content: flex-start;
          text-align: left;
          &.temperature-span {
            padding-left: 20px;
            max-width: 210px;
            margin: 0 16px 0 20px;
            border-left: 1px solid #223c88;
            font-size: 25px;
          }
        }
        img {
          width: 30px;
          margin-right: 5px;
          float: left;
        }
      }
    }
    .header-right {
      width: 29.375%;
      float: right;
      position: relative;
      .header-img-seat {
        transform: rotateY(180deg);
      }
      .header-right-message {
        width: 100%;
        height: 100%;
        position: absolute;
        left: 0;
        top: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        .header-right-content {
          display: flex;
          align-items: center;
          justify-content: center;
        }
        span {
          display: inline-block;
          height: 30px;
          line-height: 30px;
          margin-right: 12px;
        }
      }
      .header-right-button {
        height: 100%;
        position: absolute;
        right: 10px;
        top: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        .header-right-setting,
        .header-right-screen {
          width: 31px;
          margin-left: 10px;
          position: relative;
          &:hover {
            span {
              display: block;
            }
          }
          img {
            width: 100%;
            cursor: pointer;
          }
          span {
            display: none;
            padding: 11px 14px;
            font-size: 14px;
            white-space: nowrap;
            color: #f6752c;
            border: 1px solid #1072e2;
            background: rgba(7, 23, 62, 1);
            border-radius: 5px;
            position: absolute;
            top: 100%;
            right: 0%;
            margin-top: 10px;
          }
        }
      }
    }
  }
  .header-default {
    width: 100%;
    position: absolute;
    top: 0;
    left: 0;
    .header-left {
      width: 50%;
      height: 100%;
      padding: 0 0 0 10px;
      float: left;
      box-sizing: border-box;
      font-size: 12px;
      transform: translate(0, 11%);
      .header-left-img {
        width: 100%;
        display: block;
      }
      .header-left-weather {
        width: 18.48%;
        float: left;
        position: relative;
        .weather-content {
          position: absolute;
          top: 0;
          left: 0;
          width: 100%;
          height: 100%;
          .weather-content-left {
            width: 45%;
            float: left;
            height: 100%;
            .weather-img {
              height: 46%;
              display: block;
              margin: 6% auto 0;
            }
            .site {
              width: 100%;
              height: 48%;
              display: flex;
              justify-content: center;
              align-items: center;
              text-align: center;
            }
          }
          .weather-content-center {
            width: 55%;
            height: 100%;
            float: left;
            text-align: left;
            display: flex;
            align-content: center;
            align-items: center;
            flex-wrap: wrap;
            span {
              overflow: hidden;
              width: 100%;
              display: block;
            }
            .temperature {
              line-height: 23px;
              height: 26px;
              font-size: 20px;
              i {
                display: inline-block;
                font-style: normal;
                white-space: nowrap;
              }
              .temperatureI {
                display: inline-block;
                float: left;
                padding-right: 5px;
              }
              .weatherB {
                display: inline-block;
                float: left;
                overflow: hidden;
                height: 100%;
                position: relative;
              }
              .weatherI {
                font-size: 12px;
                position: absolute;
                left: 0;
                top: 0;
                &.move {
                  animation: moveWeather 8s linear infinite;
                }
              }
            }
            .pm {
              line-height: 12px;
            }
          }
        }
      }
      .header-left-date {
        width: 13.41%;
        float: left;
        position: relative;
        margin-left: 10px;
        .date-content {
          position: absolute;
          top: 0;
          left: 0;
          width: 100%;
          height: 100%;
          display: flex;
          align-items: center;
          align-content: center;
          flex-wrap: wrap;
          .day {
            width: 100%;
            font-size: 12px;
            line-height: 20px;
            text-align: center;
            letter-spacing: 2px;
          }
          .time {
            width: 100%;
            overflow: hidden;
            font-size: 16px;
            line-height: 20px;
            .week-span {
              margin-right: 5px;
            }
          }
        }
      }
    }
    .header-center {
      width: 55.16%;
      z-index: 100;
      .header-center-content {
        .company-name {
          height: 89%;
          width: 36.5%;
        }
      }
      .header-operation {
        width: 42%;
        left: 29%;
        bottom: 0;
        padding-bottom: 25px;
        transform: translate(0, 90%);
      }
    }
    .header-right {
      width: 50%;
      height: 100%;
      padding: 0 10px 0 0;
      float: right;
      text-align: right;
      transform: translate(0, 18%);
      box-sizing: border-box;
      .header-right-setting,
      .header-right-screen {
        width: 6.1%;
        display: inline-block;
        position: relative;
        margin: 0 0 0 10px;
        &:hover {
          span {
            display: block;
          }
        }
        img {
          width: 100%;
          display: block;
          cursor: pointer;
        }
        span {
          display: none;
          padding: 11px 14px;
          font-size: 14px;
          white-space: nowrap;
          color: #f6752c;
          border: 1px solid #1072e2;
          background: rgba(7, 23, 62, 1);
          border-radius: 5px;
          position: absolute;
          top: 100%;
          right: 0%;
          margin-top: 10px;
        }
      }
    }
  }
  .header-center {
    width: 44.79%;
    position: absolute;
    left: 50%;
    top: 0;
    overflow: hidden;
    transform: translate(-50%, 0);
    .header-operation {
      opacity: 0;
      transition: opacity 0.1s linear;
      width: 51.7%;
      position: absolute;
      bottom: 0;
      left: 24.15%;
      transform: translate(0, 100%);
      z-index: -1;
      .header-option-img {
        width: 100%;
      }
      .header-operation-button {
        width: 100%;
        position: absolute;
        top: 18px;
        left: 0;
        span {
          display: block;
          position: absolute;
          bottom: -16px;
          left: 20%;
          width: 89px;
          height: 25px;
          line-height: 25px;
          font-size: 12px;
          color: #a1cfff;
          cursor: pointer;
          background: url("./../../assets/images/header/header-content-button.png")
            no-repeat center;
          background-size: 100% 100%;
          .icon {
            display: inline-block;
            vertical-align: top;
            width: 14px;
            height: 14px;
            margin: 0 5px 0 0;
            position: relative;
            top: 50%;
            transform: translateY(-50%);
            background: url("./../../assets/images/header/header-edit.png")
              no-repeat center;
            background-size: 100% 100%;
          }
          &.organ-button {
            left: auto;
            right: 20%;
            .icon {
              background: url("./../../assets/images/header/header-organ.png")
                no-repeat center;
              background-size: 100% 100%;
            }
          }
        }
      }
    }
    .header-center-content {
      position: relative;
      img {
        width: 100%;
      }
      .company-name {
        position: absolute;
        top: 0;
        left: 50%;
        transform: translate(-50%, 0);
        width: 89%;
        height: 81%;
        p {
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          width: 100%;
          height: 50px;
          line-height: 50px;
          font-size: 18px;
          color: #a1cfff;
        }
      }
    }
  }
}
@media screen and (min-width: 1890px) {
  .header {
    .header-flat {
      font-size: 16px;
      .header-left {
        .header-left-message {
          span {
            &.temperature-span {
              font-size: 30px;
              max-width: 250px;
              i {
                font-size: 16px;
              }
            }
          }
        }
      }
      .header-right {
        .header-right-message {
        }
        .header-right-button {
          img {
            width: 37px;
          }
        }
      }
    }
    .header-default {
      .header-right {
        .header-right-setting,
        .header-right-screen {
          img {
            width: 57px;
          }
        }
      }
    }
  }
}

@keyframes moveWeather {
  0% {
    transform: translateX(0);
    left: 100%;
  }
  100% {
    left: 0;
    transform: translateX(-100%);
  }
}
</style>
